﻿package  {
	
	import flash.display.*;
	
	import cx.display.cache.*;
	import net.hires.debug.Stats;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	
	public class TestDocument extends MovieClip 
	{
		//------通过更改设置，观看不同效果------Start
		
		//构建数量
		private var _num:Number = 1000;
		//是否开启缓存(设置观看缓存与非缓存效果)
		private var _ifOpenCache:Boolean = true;
		//是否旋转
		private var _ifRotation:Boolean = false;
		
		//------通过更改设置，观看不同效果------End
		
		
		
		//显示列表
		private var _list:Vector.<DisplayVO> = new Vector.<DisplayVO>();
		
		//构建函数
		private var _buildFun:Function;
		
		public function TestDocument() 
		{
			//------通过更改注释，观看不同效果------Start
			
			//testSprite();//测试普通显示对象
			//testSprite360();//测试旋转显示对象
			testMovieClip();//测试时间轴动画
			
			//------通过更改注释，观看不同效果------End
			
			
			addChild( new Stats() );
		}

		
		private function testSprite():void
		{			
			if(_ifOpenCache == false)
			{
				//不缓存
				_buildFun = function f():DisplayObject
				{
					return new MyShape;
				}
			}
			else
			{			
				//缓存
				DisplayCachePool.getInstance().cacheDisplay("myShape",new MyShape);			
				_buildFun = function f():DisplayObject
				{
					return DisplayCachePool.getInstance().getSprite("myShape");
				}
			}
			
			buildDisplays();
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function testSprite360():void
		{
			//开启旋转
			_ifRotation = true;
			
			if(_ifOpenCache == false)
			{
				//不缓存
				_buildFun = function f():DisplayObject
				{
					return new MyShape;
				}
			}
			else
			{
				//缓存
				DisplayCachePool.getInstance().cacheDisplay360("myShape360",new MyShape);			
				_buildFun = function f():DisplayObject
				{
					return DisplayCachePool.getInstance().getSprite("myShape360");
				}
			}
			
			buildDisplays();
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function testMovieClip():void
		{
			if(_ifOpenCache == false)
			{
				//不缓存
				_buildFun = function f():DisplayObject
				{
					return new MyMovie;
				}
			}
			else
			{
				//缓存
				DisplayCachePool.getInstance().cacheMovieClip("myMovie",new MyMovie);			
				_buildFun = function f():DisplayObject
				{
					return DisplayCachePool.getInstance().getMovieClip("myMovie");
				}
			}
			
			buildDisplays();
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		/**
		 * 根据_num构建显示对象
		 */
		private function buildDisplays():void
		{
			for(var i:int = 0 ; i < _num ; i++)
			{
				var display:DisplayObject = _buildFun();
				addChild(display);
				_list.push(new DisplayVO(display,new Point(-5 + Math.random() * 10,-5 + Math.random() * 10)));
			}
		}
		
		/**
		 * 帧循环
		 * 循环移动所有对象
		 */
		private function onEnterFrame(e:Event):void
		{
			var len:int = _list.length;
			while(len--)
			{
				var display:DisplayObject = _list[len].display;
				var speed:Point = _list[len].speed;
				display.x += speed.x;
				display.y += speed.y;
				if(_ifRotation)
				{
					display.rotation+=3;
				}
				//环绕
				if(display.x > stage.stageWidth)
				{
					display.x = display.x - stage.stageWidth;
				}
				else
				{
					if(display.x < 0)
					{
						display.x = stage.stageWidth + display.x;
					}
				}
				if(display.y > stage.stageHeight)
				{
					display.y = display.y - stage.stageHeight;
				}
				else
				{
					if(display.y < 0)
					{
						display.y = stage.stageHeight + display.y;
					}
				}
			}
		}
	}	
}


import flash.display.DisplayObject;
import flash.geom.Point;

/**
 * 存储显示对象和移动数据的结构体
 */
class DisplayVO
{
	public var display:DisplayObject;
	public var speed:Point;
	function DisplayVO(d:DisplayObject,s:Point)
	{
		display = d;
		speed = s;
	}
}
